﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using DataAccess.Classes;

public partial class Admin_Product : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            PopulateControls();
    }
    private void PopulateControls()
    {
        int howManyPages = 0;
        string page = Request.QueryString["Page"] ?? "1";
        string firstPageUrl = Link.ToAdminProduct();
        string pagerUrl = Link.ToAdminProduct("{0}");
        repProd.DataSource = HangHoa.Paging(page, out howManyPages);
        repProd.DataBind();
        pagerTop.Show(int.Parse(page), howManyPages, firstPageUrl, pagerUrl,
        false);
        pagerBottom.Show(int.Parse(page), howManyPages, firstPageUrl, pagerUrl,
        true);
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        btnDelete.Click += new EventHandler(btnDelete_Click);
        repProd.ItemCommand += new
        RepeaterCommandEventHandler(repProd_ItemCommand);
    }
    void btnDelete_Click(object sender, EventArgs e)
    {
        string stringid = Request.Form["cid"] ?? "";
        if (stringid != "")
        {
            foreach (string id in stringid.Split(','))
            {
                ChiTietHangHoa.Delete(id);
                HangHoa.Delete(id);
            }
            PopulateControls();
        }
    }
    void repProd_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "UpdatePromo")
        {
            // CommandArgument co dang ProductID_PromoFront
            string[] args = e.CommandArgument.ToString().Split('_');
            // args[0] ==> ProductID
            // args[1] ==> PromoFront (kieu String)
            HangHoa.UpdatePromo(args[0], (args[1] == "1" ? false : true));
            PopulateControls();
        }
    }
}